<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of constructions_controller
 *
 * @author rootdd
 */
class ConstructionsController extends AppController {

    var $name = "Constructions";

    function index($predio) {

        $this->set('predio', $predio);
        $this->set('constructions', $this->Construction->find('all', array('conditions' => array('Construction.property_id' => $predio), 'fields' => array('Property.codigo', 'Construction.area', 'Construction.nombre', 'Construction.id'))));
    }

    function add($predio) {
        $this->set('est', 0);
        if (empty($this->data)) {
            $this->data['Construction']['property_id'] = $predio;
        } else {
            if ($this->Construction->save($this->data)) {
                $this->Session->setFlash('Registro adicionado exitosamente');
                $this->setAction('index', $this->data['Construction']['property_id']);
                $this->redirect( array( 'controller'=>'Constructions','action'=>'index', $this->data['Construction']['property_id'] ) );
         
            }
        }
    }

    function edit($construccion) {
        $this->set('est', 0);
        if (empty($this->data)) {
            $this->Construction->recursive = -1;
            $this->data = $this->Construction->find('first', array('conditions' => array('Construction.id' => $construccion))); 
        } else {
            if ($this->Construction->save($this->data)) {
                $this->Session->setFlash('Registro adicionado exitosamente');
                $this->redirect( array( 'controller'=>'Constructions','action'=>'index', $this->data['Construction']['property_id'] ) );
                
                }else{
                  
                      $this->data = $this->Construction->find('first', array('conditions' => array('Construction.id' => $construccion))); 
     
                }
        }
    }
    function edit_valor($construccion) {
        $this->set('est', 0);
        if (empty($this->data)) {
            $this->Construction->recursive = -1;
            $this->data = $this->Construction->find('first', array('conditions' => array('Construction.id' => $construccion),'fields'=>array('Construction.valor_adoptado','Construction.id'))); 
        } else {
            
            $id=$this->data['Construction']['id'];
            if ($this->Construction->save($this->data)) {
                $this->Session->setFlash('Registro adicionado exitosamente');
                $this->redirect( array( 'controller'=>'PropertyAppraisals','action'=>'constructions_valor_index' ) );
                }else{
                  $this->data['Construction']['id']=$id;
                }
        }
    }

    function delete($construccion, $predio) {
        if ($this->Construction->delete($construccion)) {
            $this->Session->setFlash('Construcción Borrada exitosamente');
            $this->setAction('index', $predio);
        }
    }

}

?>
